<?php
class stYoutubeToolboxcomment_model extends stmodel_2_1{

    public function __construct($config) {
        parent::__construct ( $config );
        $this->setTableName($this->prefix.'st_youtube_comment');
        $this->setPrimaryKey('id');
    }

    public function savestYoutubeToolboxComment($commentFeed, $videoId, $user_id) {
        $dataInsert = array();
        $i=0;
//        echo 'video ID:'.$videoId.'<br/><br/><br/>';
        foreach ($commentFeed as $commentEntry) {
//            echo $commentEntry->title->text.'<br/>';
            $commentId = $commentEntry->id->text;
            $id = $this->getId($commentId);
            if(!$id){
                $dataInsert[$i]['id'] = 'NULL';
                $dataInsert[$i]['video_id'] = $videoId;
                $dataInsert[$i]['user_id'] = $user_id;
                $dataInsert[$i]['comment_id'] = $commentId;
                $dataInsert[$i]['title'] = $commentEntry->title->text;
                $dataInsert[$i]['content'] = $commentEntry->content->text;
                $dataInsert[$i]['author'] = $commentEntry->author[0]->name->text;
                $dataInsert[$i]['link'] = $commentEntry->link[0]->href;
                $dataInsert[$i]['updated'] = $commentEntry->updated->text;
                $i++;
                
            }
        }
        $this->inserRecords($dataInsert) ;
        
    }

    public function inserRecords($data = '', $where = ''){
        if(count($data)>0){
            $query = "INSERT INTO $this->_tableName
                    ( id, user_id, video_id, comment_id, title, content, author, link, updated )
                    VALUES
                    ";
            $query1 = array();
            foreach ($data as $value) {
                $query1[] = $this->prepare("(%d, %d, %s, %s, %s, %s, %s, %s, %s)",
                        $value['id'], $value['user_id'], $value['video_id'], $value['comment_id'], $value['title']
                        , $value['content'], $value['author'], $value['link'], $value['updated']
                        );
            }
            $query1 = implode(', ', $query1);
            $query .= ' '.$query1;
//            echo $query.'<br/>';
            return $this->query( $query );
        }else{
            return false;
        }
    }

    public function getRecords($args = '') {
        $default_args = array(
            'fields' => '*',
            'and_where' => '',
            'or_where' => '',
            'where' => '',
            'order' => '',
            'limit' => '',
            'group_by' => '',
            'custom_query' => '',
        );
        if ($args == '')
            $args = $default_args;

        $fields = $args['fields'] ? $args['fields'] : '*';
        $and_where = $args['and_where'] ? $args['and_where'] : '';
        $or_where = $args['or_where'] ? $args['or_where'] : '';
        $custom_where = $args['where'] ? $args['where'] : '';
        $order = $args['order'] ? $args['order'] : '';
        $limit = $args['limit'] ? $args['limit'] : '';
        $group_by = $args['group_by'] ? $args['group_by'] : '';
        $custom_query = $args['custom_query'] ? $args['custom_query'] : '';
        if (!empty($custom_query)) {
            $query = $custom_query;
        } else {
            if ($fields != '*')
                $fields = implode(', ', $fields);
            //
            $where = ' WHERE ';

            if ($and_where != '')
                $where .= implode('', $and_where);
            if ($or_where != '')
                $where .= implode('', $or_where);
            if ($custom_where != '')
                $where .= $custom_where;
            if ($where == ' WHERE ')
                $where = '';
            //
            if ($order != '')
                $order = ' ORDER BY ' . implode(' ', $order);
            //
            if ($limit != '')
                $limit = ' LIMIT ' . implode(', ', $limit);
            if ($group_by != '')
                $group_by = ' GROUP BY ' . $group_by;
            //

            $table_video = $this->prefix.'st_youtubevideo';
            $table_user = $this->prefix.'st_youtube_user';
            $query = "SELECT $fields
            FROM $this->_tableName comment
            LEFT JOIN $table_video video on video.video_id = comment.video_id
            INNER JOIN $table_user user on user.id = video.user_id
            $where
            $group_by
            $order
            $limit
            ";
        }
//        echo $query;
        return $this->get_results($query);
    }

    public function getId($id){
        $query = "SELECT id
        FROM $this->_tableName
        WHERE comment_id like ('$id')
        ";
        $result = $this->get_var($query);
        return $result;
    }

    public function countRecords($args = ''){
        $result = $this->getRecords($args);
        return count($result);

    }
}